package com.xdf.xzymanagementsystem.controller;

import com.xdf.xzymanagementsystem.entity.ResponseResult;
import com.xdf.xzymanagementsystem.entity.SysPermission;
import com.xdf.xzymanagementsystem.enums.ResponseEnum;
import com.xdf.xzymanagementsystem.service.SysPermissionService;
import com.xdf.xzymanagementsystem.util.ShiroUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Description : 自动生成的控制层
 * @Author : 张柯
 * @Date : 2020-12-31 14:42:38
 */
@Api(tags = "权限表")
@CrossOrigin
@RestController
@RequestMapping("/sysPermission")
public class SysPermissionController {

    @Autowired
    private SysPermissionService service;
    @Autowired
    private ShiroUtils shiroUtils;

    /**
     * @Description : 选择新增
     * @Return : ResponseResult<Void>
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "选择新增", httpMethod = "POST", notes = "自动生成的新增方法")
    @RequestMapping(value = "/insertSelective", method = RequestMethod.POST)
    public ResponseResult<Void> insertSelective(SysPermission record) throws Exception {
        service.insertSelective(record);
        return ResponseResult.<Void>builder().success(true).message(ResponseEnum.OK.getName()).build();
    }

    /**
     * @Description : 批量新增
     * @Return : ResponseResult<Void>
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "批量新增", httpMethod = "POST", notes = "自动生成的新增方法")
    @RequestMapping(value = "/multiInsert", method = RequestMethod.POST)
    public ResponseResult<Void> multiInsert(@RequestBody List<SysPermission> list) throws Exception {
        service.multiInsert(list);
        return ResponseResult.<Void>builder().success(true).message(ResponseEnum.OK.getName()).build();
    }

    /**
     * @Description : 选择修改
     * @Return : SysPermission
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "选择修改", httpMethod = "PUT", notes = "自动生成的修改方法")
    @RequestMapping(value = "/updateSelective", method = RequestMethod.POST)
    public ResponseResult<Void> updateSelective(SysPermission record) throws Exception {
        service.updateByPrimaryKeySelective(record);
        return ResponseResult.<Void>builder().success(true).message(ResponseEnum.OK.getName()).build();
    }

    /**
     * @Description : 批量修改
     * @Return : SysPermission
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "批量修改", httpMethod = "POST", notes = "自动生成的修改方法")
    @RequestMapping(value = "/multiUpdate", method = RequestMethod.POST)
    public ResponseResult<Void> multiUpdate(@RequestBody List<SysPermission> list) throws Exception {
        service.multiUpdate(list);
        return ResponseResult.<Void>builder().success(true).message(ResponseEnum.OK.getName()).build();
    }

    /**
     * @Description : 根据主键查询
     * @Return : SysPermission
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "根据主键查询", httpMethod = "GET", notes = "自动生成的查询方法")
    @RequestMapping(value = "/queryByPk/{id}", method = RequestMethod.GET)
    public SysPermission selectByPrimaryKey(@PathVariable("id") Integer id) throws Exception {
        if (id == null) return null;
        return service.selectByPrimaryKey(id);
    }

    /**
     * @Description : 查询方法
     * @Return : java.util.List
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "查询方法", httpMethod = "GET", notes = "自动生成的查询方法")
    @RequestMapping(value = "/listQuery", method = RequestMethod.GET)
    public List<SysPermission> query(SysPermission record) throws Exception {
        return service.listQuery(record);
    }

    /**
     * @Description : 根据主键删除方法
     * @Return : ResponseResult<Void>
     * @Author : 张柯
     * @Date : 2020-12-31 14:42:38
     */
    @ApiOperation(value = "根据主键删除方法", httpMethod = "DELETE", notes = "自动生成的删除方法")
    @RequestMapping(value = "/deleteByPk/{id}", method = RequestMethod.POST)
    public ResponseResult<Void> deleteByPrimaryKey(@PathVariable("id") Integer id) throws Exception {
        service.deleteByPrimaryKey(id);
        return ResponseResult.<Void>builder().success(true).message(ResponseEnum.OK.getName()).build();
    }

    @RequestMapping(value = "/getObjectPermission/{menuId}", method = RequestMethod.GET)
    public ResponseResult<List<SysPermission>> getObjectPermission(@PathVariable("menuId") Integer menuId) {
        return ResponseResult.<List<SysPermission>>builder().success(true).message(ResponseEnum.OK.getName())
                .data(service.getObjectPermission(menuId))
                .build();
    }

    @ApiOperation(value = "在线刷新用户权限 - 清除用户权限缓存", httpMethod = "POST", notes = "自动生成的删除方法")
    @RequestMapping(value = "/refreshPermission", method = RequestMethod.POST)
    public ResponseResult<Void> refreshPermission(@RequestBody List<Integer> roleIdList) throws Exception {
        roleIdList.forEach(roleId -> shiroUtils.reloadPermissions8RoleId(roleId));
        return ResponseResult.<Void>builder().success(true).message(ResponseEnum.OK.getName()).build();
    }
}